﻿using System;
using System.Data.SqlClient;
using System.IO;
using System.Text.RegularExpressions;

public partial class Forma_9 : System.Web.UI.Page
{
    private String SessionID;
    private Boolean FolioExists;
    private Int32 Folio;

    private void Page_Load(object sender, EventArgs e)
    {
        SessionValidation();

        // Se verifíca si existe un Folio actual para cargar los datos
        // que han sido previamente guardados
        DatabaseConnection dbCon = new DatabaseConnection();
        FolioExists = dbCon.FolioExistsForCurrentUser(ref Folio, SessionID);
        dbCon.CloseConnection();
        if (FolioExists && !Page.IsPostBack)
        {
            LoadFormWithPastData();
        }
    }

    private void LoadFormWithPastData()
    {
        DatabaseConnection dbCon = new DatabaseConnection();
        SqlDataReader MotivosReader;
        String myCommand = "SELECT [Comentarios_Comite] " +
                       "FROM [wbecfildb].[dbo].[Solicitud] " +
                       "WHERE [Folio] = " + Folio + ";";

        MotivosReader = dbCon.ExecuteSelect(myCommand);
        if (MotivosReader.Read())
        {
            String comentarios = MotivosReader["Comentarios_Comite"].ToString();
            //Comentarios_Comite.Text = comentarios;
        }
        MotivosReader.Close();
    }


    protected void UploadDocuments(object sender, EventArgs e)
    {
        if (FileUploadControl.HasFile)
        {
            StatusLabel.Visible = true;
            String fileExt = Path.GetExtension(FileUploadControl.FileName).ToLower();
            try
            {
                if (FileUploadControl.PostedFile.ContentType == "application/x-rar-compressed, application/octet-stream" ||
                    FileUploadControl.PostedFile.ContentType == "application/x-7z-compressed" ||
                    FileUploadControl.PostedFile.ContentType == "application/zip, application/octet-stream" ||
                    String.Equals(fileExt, "zip") || String.Equals(fileExt, "rar") || String.Equals(fileExt, "7z"))
                {
                    if (FileUploadControl.PostedFile.ContentLength < 10240000)
                    {
                        
                        string filename = SessionID + "_" + Path.GetFileName(FileUploadControl.FileName);
                        FileUploadControl.SaveAs(Server.MapPath("/website/Uploads/") + filename);
                        StatusLabel.Attributes.CssStyle.Add("color", "#468847");
                        StatusLabel.Text = "<strong>Carga de archivos finalizada correctamente.</strong>";
                    }
                    else
                        StatusLabel.Attributes.CssStyle.Add("color", "#B94A48");
                        StatusLabel.Text = "<strong>El archivo debe ser menor de 10 Mbytes</strong>";
                }
                else
                {
                    StatusLabel.Attributes.CssStyle.Add("color", "#B94A48");
                    StatusLabel.Text = "<strong>S&oacute;lo se aceptan archivos ZIP / RAR / 7z</strong>";
                }
            }
            catch (Exception ex)
            {
                StatusLabel.Attributes.CssStyle.Add("color", "#B94A48");
                StatusLabel.Text = "<strong>Se produjo el siguiente error: " + ex.Message + "</strong>";
            }
        }
    }

    protected void InsertData(object sender, EventArgs e)
    {
        DatabaseConnection dbCon = new DatabaseConnection();
        String query;
        int rows;

        query = "UPDATE [wbecfildb].[dbo].[Solicitud] " +
                //"SET [Comentarios_Comite] = '" + Comentarios_Comite.Text + "' "+
                "WHERE [Folio] = " + Folio + ";";  
        rows = dbCon.ExecuteNonQuery(query);

        Response.Redirect("~/Forma/Resumen.aspx");
    }

    /* Valida que la sesión del usuario sea la correcta para mostrar
     *  el contenido de esta página
     */
    protected void SessionValidation()
    {
        SessionID = (String)Session["SessionID"];
        String UserType = (String)Session["UserType"];

        if (String.IsNullOrEmpty(SessionID) || String.IsNullOrEmpty(UserType))
        {
            Session.RemoveAll();
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            if (!String.Equals(UserType, "Alumnos"))
            {
                Session.RemoveAll();
                Response.Redirect("~/Default.aspx");
            }
        }
    }

}